package com.elitetyc.elitetyc_plugin;


import android.content.*;
import android.os.Bundle;
import android.util.Log;

import android.view.KeyEvent;
import android.widget.Toast;
import cn.pda.serialport.Tools;
import com.alibaba.fastjson.JSONObject;
import com.handheld.uhfr.UHFRManager;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
import com.uhf.api.cls.Reader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.Context.MODE_PRIVATE;


/**
 * uniapp 调用原生Android
 */
public class UHFR6Module extends WXModule {

    private UHFRManager uhfrManager;

    /**
     * 初始化并 开始盘点EPC
     */
    @JSMethod(uiThread = false)
    public void startUHFR(JSONObject options, JSCallback callback){
        JSONObject jsonObject = new JSONObject();
        try {
            UHFRManager uhfrManager = UHFRManager.getInstance();
            SharedPreferences sharedPreferences = this.mWXSDKInstance.getContext().getSharedPreferences("UHF", MODE_PRIVATE);
            Reader.READER_ERR err = uhfrManager.setPower(sharedPreferences.getInt("readPower", 33), sharedPreferences.getInt("writePower", 33));//set uhf module power
            if (err == Reader.READER_ERR.MT_OK_ERR) {
                uhfrManager.setRegion(Reader.Region_Conf.valueOf(sharedPreferences.getInt("freRegion", 1)));
            } else {
                Reader.READER_ERR err1 = uhfrManager.setPower(30, 30);//set uhf module power
            }
            if(uhfrManager != null){
                Reader.READER_ERR reader_err = uhfrManager.asyncStartReading();
                if(Reader.READER_ERR.MT_OK_ERR == reader_err) {
                    this.uhfrManager = uhfrManager;
                    jsonObject.put("data","初始化成功并开始盘点EPC");
                }else{
                    jsonObject.put("data","开始盘点EPC失败");
                }
            }else{
                jsonObject.put("error","uhfrManager 为空");
            }
        }catch (Exception e){
            jsonObject.put("error",e);
            if(uhfrManager != null){
                uhfrManager.asyncStopReading();
                uhfrManager.close();
            }
        }
        callback.invoke(jsonObject);
    }


    /**
     * 获取EPC
     */
    @JSMethod(uiThread = false)
    public void getData(JSONObject options, JSCallback callback){
        JSONObject jsonObject = new JSONObject();
        try {
            if(uhfrManager != null){
                List<Reader.TAGINFO> list = uhfrManager.tagInventoryRealTime();
                if(list.size()> 0){
                    String data;
                    List<Object> list1 = new ArrayList<>();
                    for (Reader.TAGINFO tfs : list) {
                        byte[] epcdata = tfs.EpcId;
                        data = Tools.Bytes2HexString(epcdata, epcdata.length);
                        Bundle b = new Bundle();
                        int rssi = tfs.RSSI;
                        b.putString("data", data);
                        b.putString("rssi", rssi + "");
                        list1.add(b);
                    }
                    jsonObject.put("list",list1);
                }
            }else{
                jsonObject.put("error","uhfrManager 为空");
            }
        }catch (Exception e){
            jsonObject.put("error",e);
            if(uhfrManager != null){
                uhfrManager.asyncStopReading();
                uhfrManager.close();
            }
        }
        callback.invoke(jsonObject);
    }

    /**
     * 关闭
     */
    @JSMethod(uiThread = false)
    public void endUHFR(JSONObject options, JSCallback callback){
        Map<String, Object> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        try{
            if(uhfrManager != null){
                uhfrManager.asyncStopReading();
                uhfrManager.close();
            }else{
                jsonObject.put("error","uhfrManager 为空");
            }
        }catch (Exception e){
            jsonObject.put("error",e);
        }
        callback.invoke(jsonObject);
    }
}
